<template>
  <view class="content">
    <view class="head">
      <view class="title">
        <image src="../../static/icon/battery.png"> </image>
        <label>电量信息</label>
      </view>
    </view>
    <view class="body">
      <view class="elec">
        <text class="name">链锁端</text>
        <view class="container">
          <view
            class="box"
            :style="{
              height: getElecHeightOne,
              backgroundColor: getElecColorOne,
            }"
          ></view>
          <view class="rela">
            <view class="line"></view>
            <view class="leftCon"></view>
            <view class="rightCon"></view>
          </view>
        </view>
        <view class="info">
          <text>当前电量:</text>
          <text>{{ `${lastPosInfo.electricOne}%` }}</text>
        </view>
      </view>
      <view class="elec">
        <text class="name">信号端</text>
        <view class="container">
          <view
            class="box"
            :style="{
              height: getElecHeightTwo,
              backgroundColor: getElecColorTwo,
            }"
          ></view>
          <view class="rela">
            <view class="line"></view>
            <view class="leftCon"></view>
            <view class="rightCon"></view>
          </view>
        </view>
        <view class="info">
          <text>当前电量:</text>
          <text>{{ `${lastPosInfo.electricTwo}%` }}</text>
        </view>
      </view>
    </view>
    <view class="bottom">
      <view class="tips">
        <label>温馨提示:</label>
        <text>电量过低时系统会自动关闭蓝牙模式</text>
      </view>
    </view>
  </view>
</template>
<script>
// 引入请求模块
import { getRequest } from "@/api/api.js";
// 引入url
import { getPos } from "@/api/request.js";

export default {
  name: "electric",
  onLoad() {
    // this.getPosInfo();
  },
  data() {
    return {
      lastPosInfo: {
        electricOne: 80,
        electricTwo: 60,
      },
      clcetric: 0,
    };
  },
  methods: {
    // 获取最后一次骑行信息
    async getPosInfo() {
      const result = await getRequest(getPos);
      console.log(result);
      if (result[1].data.status === 0) {
        if (result[1].data.data[0]) {
          this.lastPosInfo = result[1].data.data[0];
          console.log(this.lastPosInfo);
        }
      } else {
        uni.showToast({
          icon: "none",
          title: "服务器连接失败，请稍后重试！",
          duration: 2500,
        });
      }
    },
  },
  computed: {
    getElecHeightOne() {
      return `${this.lastPosInfo.electricOne * 4}rpx`;
    },
    getElecColorOne() {
      if (this.lastPosInfo.electricOne >= 70) {
        return `rgb(165, 252, 162)`;
      } else if (this.lastPosInfo.electricOne >= 40) {
        return `rgb(250, 191, 83)`;
      } else {
        return `rgb(255, 53, 53)`;
      }
    },
    getElecHeightTwo() {
      return `${this.lastPosInfo.electricTwo * 4}rpx`;
    },
    getElecColorTwo() {
      if (this.lastPosInfo.electricTwo >= 70) {
        return `rgb(165, 252, 162)`;
      } else if (this.lastPosInfo.electricTwo >= 40) {
        return `rgb(250, 191, 83)`;
      } else {
        return `rgb(255, 53, 53)`;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.content {
  position: relative;
  .head {
    height: 15vh;
    display: flex;
    justify-content: center;
    align-items: center;
    .title {
      width: 90%;
      padding: 30rpx 0;
      display: flex;
      justify-content: center;
      align-items: center;
      border-bottom: 4rpx solid rgb(192, 192, 192);
      // border-radius: 30rpx;
      image {
        width: 60rpx;
        height: 60rpx;
        opacity: 0.8;
      }
      label {
        padding-left: 20rpx;
        letter-spacing: 8rpx;
        font-size: 36rpx;
        // font-weight: bold;
        color: #595959;
      }
    }
  }
  .body {
    display: flex;
    justify-content: space-around;
    .elec {
      // padding-top: 20rpx;
      position: relative;
      height: 60vh;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .name {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100rpx;
        font-size: 36rpx;
        background-color: antiquewhite;
        border-radius: 20rpx;
        margin-bottom: 20rpx;
        letter-spacing: 4rpx;
        color: rgb(248, 184, 100);
      }
      .container {
        height: 200px;
        width: 120px;
        position: relative;
        border: 4rpx solid rgb(197, 196, 196);
        box-sizing: content-box;
        border-top: none;
        .box {
          width: 120px;
          // background-color: rgb(255, 53, 53);
          position: absolute;
          bottom: 0;
          left: 0;
          right: 0;
        }

        .rela {
          position: relative;
          width: 100%;
          height: 100%;
        }

        .leftCon {
          width: 41px;
          height: 19px;
          position: absolute;
          left: -4rpx;
          top: 0;
          background-color: white;
          border-right: 4rpx solid rgb(197, 196, 196);
          border-bottom: 4rpx solid rgb(197, 196, 196);
          border-left: 4rpx solid white;
          border-top: 4rpx solid white;
        }

        .rightCon {
          border-right: 4rpx solid white;
          border-top: 4rpx solid white;
          width: 41px;
          height: 19px;
          position: absolute;
          right: -4rpx;
          top: 0;
          background-color: white;
          border-left: 4rpx solid rgb(197, 196, 196);
          border-bottom: 4rpx solid rgb(197, 196, 196);
          border-right: 4rpx solid white;
          border-top: 4rpx solid white;
        }

        .line {
          width: 40px;
          height: 4rpx;
          background-color: rgb(197, 196, 196);
          position: absolute;
          top: 0;
          left: 40px;
        }
      }
      .info {
        width: 100%;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        text-align: center;
        display: flex;
        flex-direction: column;
        p {
          color: #595959;
        }
        text {
          color: #493c01;
          font-size: 36rpx;
        }
      }
    }
  }
  .bottom {
    height: 20vh;
    display: flex;
    justify-content: center;
    align-items: center;
    .tips {
      padding: 30rpx 30rpx;
      border: 2rpx solid #b6b6b5;
      display: flex;
      justify-content: space-around;
      align-items: center;
      border-radius: 20rpx;
      label {
        color: #888888;
        font-weight: bold;
      }
      text {
        padding-left: 6rpx;
        color: #595959;
      }
    }
  }
}
</style>